﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/11/17 2:06:59
 * ************************************************************/
namespace Mozlite.Web.Controls
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    using System.Web.Mvc;
    using Mozlite.Common;
    using Mozlite.Web.UI.Areas.UCenter;

    /// <summary>
    /// 编辑器扩展类。
    /// </summary>
    public static class EditorExtensions
    {
        /// <summary>
        /// 添加KindEditor代码样式和脚本。
        /// </summary>
        /// <param name="helper">HTML辅助类型。</param>
        /// <returns>返回代码样式和脚本。</returns>
        public static MvcHtmlString AddKECodeScript(this HtmlHelper helper)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<link rel=\"stylesheet\" href=\"{0}\" />", Globals.GetUrl("_utilities/editor/plugins/code/prettify.css"));
            sb.AppendLine("<script type=\"text/javascript\" charset=\"utf-8\" src=\"{0}\"></script>", Globals.GetUrl("~/_utilities/editor/plugins/code/prettify.js"));
            sb.AppendLine("<script type=\"text/javascript\">$(function(){ prettyPrint(); });</script>");
            return MvcHtmlString.Create(sb.ToString());
        }
        /// <summary>
        /// 添加KindEditor编辑器样式和脚本。
        /// </summary>
        /// <param name="helper">HTML辅助类型。</param>
        /// <param name="selector">编辑器的ID。</param>
        /// <returns>返回编辑器样式和脚本。</returns>
        public static MvcHtmlString AddKindEditor(this HtmlHelper helper, string selector)
        {
            var permission = Permissions.GetPermission<UCenterPermission>();
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<script type=\"text/javascript\" charset=\"utf-8\" src=\"{0}\"></script>", Globals.GetUrl("~/_utilities/editor/kindeditor-min.js"));
            sb.AppendLine("<script type=\"text/javascript\" charset=\"utf-8\" src=\"{0}\"></script>", Globals.GetUrl("~/_utilities/editor/lang/zh_CN.js"));
            sb.AppendLine(@"<script type=""text/javascript"">");
            sb.AppendLine(@"KindEditor.ready(function(K) {");
            sb.AppendLine(@"  var editor1 = K.create('{0}', {{", selector);
            sb.AppendLine(@"	cssPath : '{0}',", Globals.GetUrl("content/editor/plugins/code/prettify.css"));
            if (permission.AllowUpload == true)
            {
                sb.AppendLine(@"    uploadJson : '{0}',", UCenterUrls.DialogUpdateFileView);
                sb.AppendLine(@"    fileManagerJson : '{0}',", UCenterUrls.DialogFileManager);
            }
            sb.AppendLine(@"    resizeType : 1,");
            sb.AppendLine(@"    allowImageUpload : {0},", permission.AllowUploadImage.GetLowerString("false"));
            sb.AppendLine(@"    allowFlashUpload : {0},", permission.AllowUploadFlash.GetLowerString("false"));
            sb.AppendLine(@"    allowMediaUpload : {0},", permission.AllowUploadMedia.GetLowerString("false"));
            sb.AppendLine(@"    allowFileManager : {0}", permission.AllowFileBrowser.GetLowerString("false"));
            sb.AppendLine(@"  });");
            sb.AppendLine(@"});");
            sb.AppendLine(@"</script>");
            return MvcHtmlString.Create(sb.ToString());
        }
        /// <summary>
        /// 添加KindEditor编辑器样式和脚本。
        /// </summary>
        /// <param name="helper">HTML辅助类型。</param>
        /// <param name="selector">编辑器的ID。</param>
        /// <returns>返回编辑器样式和脚本。</returns>
        public static MvcHtmlString AddSimpleKindEditor(this HtmlHelper helper, string selector)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<script type=\"text/javascript\" charset=\"utf-8\" src=\"{0}\"></script>", Globals.GetUrl("~/_utilities/editor/kindeditor-min.js"));
            sb.AppendLine("<script type=\"text/javascript\" charset=\"utf-8\" src=\"{0}\"></script>", Globals.GetUrl("~/_utilities/editor/lang/zh_CN.js"));
            sb.AppendLine(@"<script type=""text/javascript"">");
            sb.AppendLine(@"KindEditor.ready(function(K) {");
            sb.AppendLine(@"  var editor1 = K.create('{0}', {{", selector);
            sb.AppendLine(@"    resizeType : 1,");
            sb.AppendLine(@"    allowImageUpload : false,");
            sb.AppendLine(@"    allowFlashUpload : false,");
            sb.AppendLine(@"    items : ['fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', '|', 'emoticons', 'image', 'flash', 'link']");
            sb.AppendLine(@"  });");
            sb.AppendLine(@"});");
            sb.AppendLine(@"</script>");
            return MvcHtmlString.Create(sb.ToString());
        }
    }
}